package com.cdwm.mrp.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdwm.mrp.common.tools.LayuiResponse;
import com.cdwm.mrp.common.tools.ResultVO;
import com.cdwm.mrp.domain.po.User;
import com.cdwm.mrp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @version v1.0
 * @Author: lyw
 * @Date: 2021/8/3
 */
@Controller
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userServices;


    /**
     * 查询所有用户信息
     * @param user
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/user/getUserList")
    @ResponseBody
    public LayuiResponse<?> getUserList(
            User user,
            @RequestParam(required = false,defaultValue = "1") Integer page,
            @RequestParam(required = false,defaultValue = "5") Integer limit
            ){

        HashMap<Object, Object> hashMap = new HashMap<>();
        Page<User> userPage = new Page<>(page,limit);
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if( user != null ){
            if(user.getUsername() != null && user.getUsername().length() >0)
                userLambdaQueryWrapper.eq(User::getUsername,user.getUsername());
            if (user.getAge() != null)
                userLambdaQueryWrapper.eq(User::getAge,user.getAge());
            if( user.getEmail() != null && user.getEmail().length() > 0 )
                userLambdaQueryWrapper.eq(User::getEmail,user.getEmail());
        }
        IPage<User> page1 = userServices.page(userPage,userLambdaQueryWrapper);

        return LayuiResponse.ok(page1.getRecords(),page1.getTotal());
    }


    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/user/saveUser")
    @ResponseBody
    public ResultVO<?> saveUser(@RequestBody User user){
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));

        boolean saveStatus = userServices.save(user);
        if (saveStatus){
            return ResultVO.ok();
        }
        return  ResultVO.fail();
    }


    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping("/user/editUser")
    @ResponseBody
    public ResultVO<?> editUser(@RequestBody User user){

        boolean editStatus = userServices.updateById(user);

        if (editStatus){
            return  ResultVO.ok();
        }
        return  ResultVO.fail();
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/user/removeUser/{id}")
    @ResponseBody
    public ResultVO<?> removeUser(@PathVariable  Long id){

        boolean removeStatus = userServices.removeById(id);

        if (removeStatus){
            return  ResultVO.ok();
        }
        return  ResultVO.fail();
    }

    /**
     * 批量删除用户
     * @param id
     * @return
     */
    @PostMapping("/user/batchRemove/{id}")
    @ResponseBody
    public ResultVO<?> batchRemove(@PathVariable  Long[] id){

        boolean removeStatus = userServices.removeByIds(Arrays.asList(id));

        if (removeStatus){
            return  ResultVO.ok();
        }
        return  ResultVO.fail();
    }

}
